package com.example.demo.util;

import com.example.demo.entity.Device;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class HttpClient {
    public JSONObject client(String url, HttpMethod method, MultiValueMap<String,String> param){
        RestTemplate template = new RestTemplate();
        ResponseEntity<String> responseEntity = template.getForEntity(url,String.class);
        return JSONObject.fromObject(responseEntity.getBody());


    }
    public JSONObject getJson(String url){
        HttpClient httpClient = new HttpClient();
        HttpMethod method = HttpMethod.GET;
        MultiValueMap<String,String> param = new LinkedMultiValueMap<>();
        return  httpClient.client(url,method,param);

    }

    public RS<Device> getDevice(String deviceId){
        Device device = new Device();
        HttpClient httpClient = new HttpClient();
        JSONObject jsonObject = httpClient.getJson("http://yunxin.device.guangbao-uni.com:61101/clients");
        if(jsonObject.getInt("code")!=200){
            return new RS<>("Json数据获取失败",500,null);
        }
        JSONObject value = null;
        JSONArray devices = jsonObject.getJSONArray("values");
        for(int i = 0;i<devices.size();i++){
            if(deviceId.equals(devices.getJSONObject(i).getString("device_id"))){
                value=devices.getJSONObject(i);
                break;
            }
        }
        if(value==null){
            return new RS<>("Json中不存在您的设备信息",404,null);
        }

        /*
        获取conn_time
         */
        Date date;
        String time = value.getString("conn_time").split("\\.")[0].trim();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(time);
            device.setConnTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return new RS<>("conn_time时间获取失败",500,device);
        }
        /*String deviceId;*/
        device.setDeviceId(deviceId);
        /*
        deviceId2
         */
        String deviceId2=value.getString("id");
        device.setDeviceId2(deviceId2);
        /*
        lastRes
         */
        Date lastRes;
        try {
            lastRes = sdf.parse(value.getString("last_res").split("\\.")[0].trim());
            device.setLastRes(lastRes);
        } catch (ParseException e) {
            e.printStackTrace();
            return new RS<>("last_res时间获取失败",500,device);
        }
        /*
        net
         */
        String net;
        net=value.getString("net");
        device.setNet(net);
        /*
        vail
         */
        int vail;
        vail = "ture".equals(value.getString("valid"))? 1:0;
        device.setVail(vail);

        if("null".equals(value.getString("state"))){
            return new RS<>("state:null暂时没有您状态信息", 300, device);
        }

        JSONObject state = value.getJSONObject("state");
        /*
        timestamp
         */
        long timestamp;
        timestamp = state.getLong("timestamp");
        device.setTimestamp(timestamp);
        /*
        gps
         */
        int gps;
        gps = state.getString("gps").equals("true")? 1:0;
        device.setGps(gps);

        /*
        latitude
         */
        int latitude;
        latitude = state.getInt("latitude");
        device.setLatitude(latitude);
        /*
        longitude
         */
        int longitude;
        longitude = state.getInt("longitude");
        device.setLongitude(longitude);

        /*
        direction
         */
        int direction;
        direction = state.getInt("direction");
        device.setDirection(direction);

        /*
        speed
         */
        int speed;
        speed = state.getInt("speed");
        device.setSpeed(speed);

        /*
        altitude
         */
        int altitude;
        altitude = state.getInt("altitude");
        device.setAltitude(altitude);

        /*
        satelliteCount
         */
        int satelliteCount;
        satelliteCount = state.getInt("satellite_count");
        device.setSatelliteCount(satelliteCount);

        /*
        gsmSignal
         */
        int gsmSignal;
        gsmSignal = state.getInt("gsm_signal");
        device.setGsmSignal(gsmSignal);

        /*
        power
         */
        int power;
        power = state.getInt("power");
        device.setPower(power);

        /*
        step
         */
        int step;
        step=state.getInt("step");
        device.setStep(step);

         /*
         turn
          */
        int turn;
        turn = state.getInt("turn");
        device.setTurn(turn);

        if("null".equals(state.getString("device_state"))){
            return new RS<Device>("device_state:null暂时没有您设备的状态信息!",500,device);
        }
        JSONObject deviceState=state.getJSONObject("device_state");

         /*
         lowPower
          */
        int lowPower;
        lowPower = "true".equals(deviceState.getString("low_power"))?1:0;
        device.setLowPower(lowPower);

         /*
         wear
          */
        int wear;
        wear = "true".equals(deviceState.getString("wear"))?1:0;
        device.setWear(wear);

        /*
        sos
         */
        int sos;
        sos = "true".equals(deviceState.getString("sos"))?1:0;
        device.setSos(sos);

        /*
        low_power_warn
         */
        int lowPowerWarn;
        lowPowerWarn = "true".equals(deviceState.getString("low_power_warn"))?1:0;
        device.setLowPowerWarn(lowPowerWarn);

         /*
         break_warn
          */
        int breakWarn;
        breakWarn = "true".equals(deviceState.getString("break_warn"))?1:0;
        device.setBreakWarn(breakWarn);

         /*
         fall_warn
          */
        int fallWarn;
        fallWarn= "true".equals(deviceState.getString("fall_warn"))?1:0;
        device.setFallWarn(fallWarn);

         /*
         BSL_count
          */
        int BSLCount;
        BSLCount = state.getInt("BSL_count");
        device.setBSLCount(BSLCount);

        /*

         */
        int gmsDely;
        gmsDely = state.getInt("gms_dely");
        device.setGmsDely(gmsDely);

         /*
         MCC
          */
        int MCC;
        MCC = state.getInt("MCC");
        device.setMCC(MCC);

         /*
         MNC
          */
        int MNC;
        MNC = state.getInt("MNC");
        device.setMNC(MNC);

        if(state.getJSONArray("BSLs").size()==0){
            return new RS<>("BSLs信息为空!",300,device);
        }
        JSONObject bsls = state.getJSONArray("BSLs").getJSONObject(0);
         /*
         LAC
          */
        int LAC;
        LAC = bsls.getInt("LAC");
        device.setLAC(LAC);

         /*
         CID
          */
        int CID;
        CID = bsls.getInt("CID");
        device.setCID(CID);

         /*
         BSSS
          */
        int BSSS;
        BSSS = bsls.getInt("BSSS");
        device.setBSSS(BSSS);

         /*
         wifi_count
          */
        int wifiCount;
        wifiCount = state.getInt("wifi_count");
        device.setWifiCount(wifiCount);

        return new RS<>("device创建成功!",200,device);


    }

    public void run(){

    }

}